package iosr11.gbase.dao;

import iosr11.data.EventKind;
import iosr11.data.EventSeries;
import iosr11.data.Student;
import iosr11.data.Teacher;

import java.util.List;
import java.util.logging.Logger;

import com.google.api.gbase.client.GoogleBaseEntry;
import com.google.gdata.data.PlainTextConstruct;

public class EventSeriesDAOImpl extends GenericDAOImpl<EventSeries> implements EventSeriesDAO {

	private static String TYPE = "Event series";
	private static String KIND_NAME = "kind";
	private static String TEACHER_REF = "teacher";
	private Logger log = Logger.getLogger("service-log");
	
	@Override
	public List<EventSeries> getByKind(EventKind kind) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<EventSeries> getByStudent(Student student) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean checkType(GoogleBaseEntry entry) {
		return entry.getGoogleBaseAttributes().getItemType().equals(TYPE);
	}

	/*
	@Override
	public List<EventSeries> getAll() {
		// TODO Auto-generated method stub
		return null;
	}//*/
	
	@Override
	public Class<EventSeries> getEntityClass() {
		// TODO Auto-generated method stub
		return null;
	}
	

	@Override
	public EventSeries rewrite(GoogleBaseEntry entry) {
		EventSeries series = new EventSeries();
		series.setId(entry.getId());
		series.setKind(entry.getGoogleBaseAttributes().getIntAttribute(KIND_NAME));
		
		if (entry.getGoogleBaseAttributes().getTextAttribute(TEACHER_REF).contains("http")){
			log.info("jest ref na teacher'a - ustawianie");
			Teacher teacher = new Teacher();
			teacher.setId(entry.getGoogleBaseAttributes().getTextAttribute(TEACHER_REF));
			series.setTeacher(teacher);
		}
		else {
			log.info("brak ref na nauczyciela -null");
			series.setTeacher(null);
		}
		return series;
	}

	@Override
	public GoogleBaseEntry rewrite(EventSeries entity) {
		GoogleBaseEntry entry = new GoogleBaseEntry();
		entry.setTitle(new PlainTextConstruct(TYPE));
		entry.setDraft(true);
		entry.getGoogleBaseAttributes().setItemType(TYPE);
		entry.getGoogleBaseAttributes().addIntAttribute(KIND_NAME, entity.changeEventKindToInt(entity.getKind()));
		if (entity.getTeacher()!=null){
			if (entity.getTeacher().getId()!=null){
				log.info("ustawianie refernecji na nauczyciela");
				entry.getGoogleBaseAttributes().addTextAttribute(TEACHER_REF, entity.getTeacher().getId());
				return entry;
			}
		}
		log.info("brak referencji na teachera ustawianie null");
		entry.getGoogleBaseAttributes().addTextAttribute(TEACHER_REF, "null");
		return entry;
	}

}
